package com.derbysoft.dswitch.adapter.ihg.dswitch2.translate;

import com.derbysoft.dswitch.adapter.ihg.common.domain.HotelMapping;
import com.derbysoft.dswitch.adapter.ihg.common.utils.EnvironmentUtils;
import com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.Translator;
import com.derbysoft.dswitch.adapter.ihg.dto.AvailRequestSegment;
import com.derbysoft.dswitch.adapter.ihg.dto.AvailRequestSegments;
import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRQ;
import com.derbysoft.dswitch.adapter.ihg.dto.RoomStayCandidates;
import com.derbysoft.dswitch.adapter.ihg.webservice.impl.RequestPair;
import com.derbysoft.remote.dswitch.core.GenericRQ;
import com.derbysoft.remote.dswitch.hotel.dto.HotelMultiAvailRQ;
import com.derbysoft.remote.dswitch.hotel.dto.HotelRefDTO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component("ds2HotelMultiAvailRQTranslatorV2")
public class HotelMultiAvailRQTranslatorV2 extends BaseTranslator
        implements Translator<GenericRQ<HotelMultiAvailRQ>, List<RequestPair<OTAHotelAvailRQ>>, Void> {

    @Override
    public List<RequestPair<OTAHotelAvailRQ>> translate(GenericRQ<HotelMultiAvailRQ> hotelMultiAvailRQ, Void aVoid) {
        List<RequestPair<OTAHotelAvailRQ>> availRequestPairs = new ArrayList<RequestPair<OTAHotelAvailRQ>>();
        for (HotelRefDTO hotelRefDTO : hotelMultiAvailRQ.getPayload().getHotels()) {
            OTAHotelAvailRQ otaHotelAvailRQ = new OTAHotelAvailRQ();
            setHeader(otaHotelAvailRQ, hotelMultiAvailRQ.getHeader());
            HotelMapping hotelMapping = loadHotel(hotelRefDTO.getCode());
            otaHotelAvailRQ.setAvailRequestSegments(translateAvailRequestSegments(hotelMapping, hotelMultiAvailRQ));
            availRequestPairs.add(new RequestPair<OTAHotelAvailRQ>(otaHotelAvailRQ, hotelMapping.getDerbyCode()));
        }
        return availRequestPairs;
    }

    private AvailRequestSegments translateAvailRequestSegments(HotelMapping hotelMapping, GenericRQ<HotelMultiAvailRQ> genericHotelMultiAvailRQ) {
        HotelMultiAvailRQ hotelMultiAvailRQ = genericHotelMultiAvailRQ.getPayload();
        AvailRequestSegment availRequestSegment = new AvailRequestSegment();
        availRequestSegment.setAvailReqType(EnvironmentUtils.getIhgAvailabilityRequestType());
        availRequestSegment.setHotelSearchCriteria(createHotelSearchCriteria(hotelMapping.getDerbyCode()));
        availRequestSegment.setStayDateRange(translateStayDateRange(hotelMultiAvailRQ.getStayDateRange()));
        availRequestSegment.setRoomStayCandidates(createRoomStayCandidates(hotelMultiAvailRQ));
        availRequestSegment.setRatePlanCandidates(createRatePlanCandidates(genericHotelMultiAvailRQ.getHeader().getSource().getUniqueID().getId()));
        AvailRequestSegments availRequestSegments = new AvailRequestSegments();
        availRequestSegments.getAvailRequestSegment().add(availRequestSegment);
        return availRequestSegments;
    }

    private RoomStayCandidates createRoomStayCandidates(HotelMultiAvailRQ hotelMultiAvailRQ) {
        RoomStayCandidates roomStayCandidates = new RoomStayCandidates();
        roomStayCandidates.getRoomStayCandidate()
                .addAll(translateRoomStayCandidates(hotelMultiAvailRQ.getRoomStayCandidates()));
        return roomStayCandidates;
    }
}

